/***************
 * Import Sources Panel
 */
AIR2.Import.Sources = function() {
    var panel;
    var statusFilter = new AIR2.UI.ComboBox({
        value: '',
        choices: [
            ['', 'All'],
            ['C', 'Conflicts'],
            ['E', 'Errors'],
            ['D', 'Complete']
        ],
        width: 80,
        listeners: {
            select: function(fld, rec, idx) {
                panel.store.setBaseParam('status', rec.data.value);
                panel.store.load();
            }
        }
    });

    panel = new AIR2.UI.PagingPanel({
        pageSize: 25,
        colspan: 2,
        rowspan: 2,
        title: 'Import Sources',
        cls: 'air2-import-sources',
        iconCls: 'air2-icon-source',
        showTotal: true,
        url: AIR2.Import.TANKURL+'/sources',
        storeData: AIR2.Import.TSRCDATA,
        itemSelector: '.air2-import-source-row',
        tools: ['->', '<b>Show:</b>', statusFilter],
        tpl: new Ext.XTemplate(
            '<table>' +
              '<tr>' +
                '<th>Username</th>' +
                '<th>First</th>' +
                '<th>Last</th>' +
                '<th>Status</th>' +
              '</tr>' +
              '<tpl for="."><tr class="air2-import-source-row">' +
                '<tpl if="Source"><td>{[AIR2.Format.sourceName(values.Source,true)]}</td></tpl>' +
                '<tpl if="!Source"><td>{src_username}</td></tpl>' +
                '<td>{src_first_name}</td>' +
                '<td>{src_last_name}</td>' +
                '<td>{[this.formatStatus(values)]}</td>' +
              '</tr></tpl>' +
            '</table>',
            {
                compiled:true,
                disableFormats:true,
                formatStatus: function(values) {
                    if (values.tsrc_status == 'C') {
                        return 'C <a class="resolve">resolve</a>';
                    }
                    else {
                        return values.tsrc_status;
                    }
                }
            }
        )
    });

    // add listener for resolver
    panel.getBody().get(0).on('click', function(dv, idx, node, ev) {
        if (ev.getTarget('a.resolve')) {
            var rec = dv.getRecord(node);
            var w = AIR2.Import.Resolve({
                url: AIR2.Import.TANKURL+'/sources/'+rec.data.tsrc_id,
                originEl: node
            });
            w.on('close', function() {
                panel.reload();
                Ext.getCmp('air2-import-summary-panel').reload();
            });
        }
    });

    return panel;
}